# -*- coding: utf-8 -*-
# @Time   : 2023/9/15 15:01
# @Author : 大龙 🚀
# @File   : ningbo_illegal.py
import json
import re

import scrapy

from apps.tax_illegal.tax_illegal.items import NetTaxIllegalProItem
from apps.tax_illegal.tax_illegal.spiders import BaseTaxIllegalSpider
from utils.tools import urlencode


class NingboIllegalSpider(BaseTaxIllegalSpider):
    name = "ningbo_illegal"
    province: str = "宁波"
    url: str = "http://ningbo.chinatax.gov.cn/zdsswfsxztxxgbl/index.html"  # 取首页

    def start_requests(self):
        yield from self.etax_search(**{"etax_url": "https://etax.ningbo.chinatax.gov.cn:8443"})
        url = "http://ningbo.chinatax.gov.cn/api-gateway/jpaas-publish-server/front/page/build/unit"
        params = {
            "webId": "GL1AZq0tvpJqDvyYhAye4",
            "pageId": "kIynY2HSIYp1WmHLZW982",
            "parseType": "preview",
            "pageType": "column",
            "tagId": "检索内容",
            "tplSetId": "d0XPiicnmCyzKBop1M4um",
            "unitUrl": "/api-gateway/jpaas-publish-server/front/page/build/unit",
            "paramJson": json.dumps(
                {
                    "pageNo": 1,
                    "loadEnabled": True,
                    "search": json.dumps(
                        {
                            "field_1900": "",
                            "field_1865": "",
                            "field_1866": "宁波",
                            "field_1899": "",
                            "field_1867": "",
                            "field_1870": "",
                            "field_1872": "",
                            "field_1875": "",
                            "deployTime": "",
                            "field_1884": "",
                            "field_1886": "",
                            "field_1887": "",
                            "field_1889": "",
                            "field_1890": "",
                            "field_1901": "",
                            "field_1902": "",
                            "field_2322": "",
                            "field_2323": "",
                            "field_1891": "",
                        }
                    ),
                    "pageSize": "15",
                }
            ),
        }
        yield self.Request(url + "?" + urlencode(params), callback=self.parse, cb_kwargs={"page_no": 1})

    def parse(self, response, **kwargs):
        html = response.json()["data"]["html"]
        count = scrapy.Selector(text=html).xpath(".").re_first(r"count':'(\d+)'")
        for url in scrapy.Selector(text=html).xpath("//a/@href").getall():
            yield response.follow(url, callback=self.parse_detail)

        page_no = kwargs.get("page_no")
        url = "http://ningbo.chinatax.gov.cn/api-gateway/jpaas-publish-server/front/page/build/unit"
        if int(count) > page_no * 15:
            page_no = page_no + 1
            params = {
                "webId": "GL1AZq0tvpJqDvyYhAye4",
                "pageId": "kIynY2HSIYp1WmHLZW982",
                "parseType": "preview",
                "pageType": "column",
                "tagId": "检索内容",
                "tplSetId": "d0XPiicnmCyzKBop1M4um",
                "unitUrl": "/api-gateway/jpaas-publish-server/front/page/build/unit",
                "paramJson": json.dumps(
                    {
                        "pageNo": page_no,
                        "loadEnabled": True,
                        "search": json.dumps(
                            {
                                "field_1900": "",
                                "field_1865": "",
                                "field_1866": "宁波",
                                "field_1899": "",
                                "field_1867": "",
                                "field_1870": "",
                                "field_1872": "",
                                "field_1875": "",
                                "deployTime": "",
                                "field_1884": "",
                                "field_1886": "",
                                "field_1887": "",
                                "field_1889": "",
                                "field_1890": "",
                                "field_1901": "",
                                "field_1902": "",
                                "field_2322": "",
                                "field_2323": "",
                                "field_1891": "",
                            }
                        ),
                        "pageSize": "15",
                    }
                ),
            }
            yield self.Request(url + "?" + urlencode(params), callback=self.parse, cb_kwargs={"page_no": page_no})

    def parse_detail(self, response, **kwargs):
        item = NetTaxIllegalProItem()

        info = []
        for i in response.xpath('//table[@border="1"]//tr'):
            res = [i.xpath("string(.)").get().strip() for i in i.xpath(".//td")]
            if res:
                if len(res) != 2:
                    continue
                info.append(res)

        item.province = self.province
        for k, *v in info:
            v = " ".join(v)
            k = re.sub(r"[^\u4e00-\u9fa5]+", "", k)
            if "失信主体名称" in k:
                v = re.sub(r"(<!--.*?-->|<.*?>|\s*)", "", v, flags=re.S)
                item.company_name = v
            if "失信主体纳税人识别号或社会信用代码" in k:
                v = re.sub(r"(<!--.*?-->|<.*?>|\s*)", "", v, flags=re.S)
                item.taxpayer_id = v
            if "组织机构代码" in k:
                v = re.sub(r"(<!--.*?-->|<.*?>|\s*)", "", v, flags=re.S)
                item.org_code = v
            if "注册地址" in k:
                v = re.sub(r"(<!--.*?-->|<.*?>|\s*)", "", v, flags=re.S)
                item.city = item.resolve_register_address(v)[0]
                item.country = item.resolve_register_address(v)[1]
                item.address = v
            if "法定代表人或者负责人姓名性别及身份证号码或其他证件号码" in k:
                v = re.sub(r"(<!--.*?-->|<.*?>|\s*)", "", v, flags=re.S)
                item.legal_representative = v
            if "经法院裁判确定的实际责任人姓名性别及身份证号码或其他证件号码" in k:
                v = re.sub(r"(<!--.*?-->|<.*?>|\s*)", "", v, flags=re.S)
                item.resp_person = v
            if "违法期间法人代表或者负责人姓名性别及身份证号码或其他证件号码" in k:
                v = re.sub(r"(<!--.*?-->|<.*?>|\s*)", "", v, flags=re.S)
                item.illegal_legal_representative = v
            if "负有直接责任的财务人员姓名性别及身份证号码或其他证件号码" in k:
                v = re.sub(r"(<!--.*?-->|<.*?>|\s*)", "", v, flags=re.S)
                item.resp_financial = v
            if "负有直接责任的中介机构信息" in k:
                v = re.sub(r"(<!--.*?-->|<.*?>|\s*)", "", v, flags=re.S)
                item.resp_intermediary = v
            if "失信主体违法性质" in k:
                v = re.sub(r"(<!--.*?-->|<.*?>|\s*)", "", v, flags=re.S)
                item.illegal_status = v
            if "主要违法事实" in k:
                v = re.sub(r"(<!--.*?-->|\s*|<.*?>)", "", v, flags=re.S)
                illegal_facts_src = re.sub(r"(<.*?>|\s*|“|”)", "", re.sub(r"(<!--.*?-->)", "", v))
                item.illegal_facts = illegal_facts_src
            if "相关法律依据及税务处理处罚情况" in k:
                v = " ".join(re.sub(r"(<!--.*?-->|<.*?>)", "", v, flags=re.S).split())
                item.basis_and_punishment = v
        pub_date = response.xpath('//*[@name="PubDate"]/@content').get()
        item.year = pub_date[:10].replace("-", "")
        yield item


if __name__ == "__main__":

    from scrapy import cmdline

    cmdline.execute(argv=["scrapy", "crawl", "ningbo_illegal"])
